package com.example.androidcomplete0102.ch12;

import java.util.ArrayList;

import com.example.androidcomplete0102.R;

import android.app.Activity;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class ListViewMutiple extends Activity {
	ArrayList<String> arrItems;
	ArrayAdapter<String> adapter;
	ListView listview;
	EditText et;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch12_listviewadddel);
		
		et = (EditText)findViewById(R.id.ch12_edit_newitem);
		arrItems = new ArrayList<>();
		arrItems.add("First");
		arrItems.add("Second");
		arrItems.add("Third");
		
		
		adapter = new ArrayAdapter<>(this, 
				android.R.layout.simple_list_item_multiple_choice, arrItems);
				
		listview = (ListView)findViewById(R.id.ch12_listviewadddle);
		listview.setAdapter(adapter);
		listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
					String text = "Select Item: "+ arrItems.get(position);
					Toast.makeText(getApplicationContext(), 
							text, Toast.LENGTH_LONG).show();
			}
		});
	}
	
	public void mOnClick(View v){
		switch(v.getId()){
		case R.id.ch12_btn_add:
			String text = et.getText().toString();
			arrItems.add(text);
			adapter.notifyDataSetChanged();
			break;
		case R.id.ch12_btn_del:
			SparseBooleanArray sb = listview.getCheckedItemPositions();
			//sb ={false, true, false...}
			if(sb.size() != 0){
				for(int i=listview.getCount()-1; i>-1; i--){
					if(sb.get(i)){
						arrItems.remove(i);
;					}
				}
				listview.clearChoices();
				adapter.notifyDataSetChanged();
			}
			break;
		}
	}
}
